package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.adapter.CarUsedListAdapter;
import com.isunland.managesystem.base.Base;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.BaseParams;
import com.isunland.managesystem.entity.CarUsed;
import com.isunland.managesystem.entity.CarUsedOriginal;
import com.isunland.managesystem.entity.CarUsedParams;
import com.isunland.managesystem.entity.CurrentUser;
import com.isunland.managesystem.entity.ExtraValueContent;
import com.isunland.managesystem.utils.MyDateUtil;
import com.isunland.managesystem.utils.ParamsNotEmpty;
import com.isunland.managesystem.utils.ToastUtil;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * 对象功能:
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: HuXiaoDan
 * 创建时间: 2017/4/6 14:45
 */
public class CarUsedListFragment extends BaseListFragment {
  public static final String EXTRA_PARAMS =
      CarUsedListFragment.class.getSimpleName() + "EXTRA_PARAMS";
  public static final String EXTRA_CONTENT =
      CarUsedListFragment.class.getSimpleName() + "EXTRA_CONTENT";
  private final int REQUEST_DETAIL = 0;//添加,详情
  private final int REQUEST_QUERY = 1;//查询

  private ArrayList<CarUsed> mlist;
  private String mBeginDate;
  private String mEndDate;
  private String mCarNumber;
  private String mCarManName;
  /** 参数 */
  private CarUsedParams mParams;
  /** 主键id */
  private String mMainId;
  /** 表名 */
  private String mMainName;
  /** 来自哪 */
  private int mFrom;
  private CarUsed mContent;
  private CarUsedListAdapter madapter;

  public static Fragment newInstance(BaseParams params, CarUsed content) {
    CarUsedListFragment fragment = new CarUsedListFragment();
    Bundle args = new Bundle();
    args.putSerializable(EXTRA_PARAMS, params);
    args.putSerializable(EXTRA_CONTENT, content);
    fragment.setArguments(args);
    return fragment;
  }

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    initData();
    setIsPaging(true);
  }

  public void initData() {
    setBack(true);
    setTitleCustom("车辆使用情况");
    mlist = new ArrayList<>();
    mBeginDate = MyDateUtil.parseDateToString(MyDateUtil.getBeforeMonth(1));
    mEndDate = MyDateUtil.parseDateToString(MyDateUtil.getAfterMonth(1));
    mCarNumber = "";
    mCarManName = "";
    if (getArguments() != null) {
      mParams = (CarUsedParams) getArguments().getSerializable(EXTRA_PARAMS);
      mContent = (CarUsed) getArguments().getSerializable(EXTRA_CONTENT);
      if (mParams != null) {
        mMainId = mParams.getMainId();
        mMainName = mParams.getMainNames();
        mFrom = mParams.getFrom();
      }
    }
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

    inflater.inflate(R.menu.menu_add_search, menu);
    //如果是车辆申请的车辆登记,取消查询
    // 如果是作业任务,取消查询
    if (mParams != null && (CarUsedParams.CAR_APPLY == mParams.getFrom())) {
      menu.getItem(1).setVisible(false);
    }
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        getActivity().finish();
        return true;
      case R.id.menu_item_add:
        //添加
        Intent intent = new Intent(getActivity(), CarUsedDetailActivity.class);
        intent.putExtra(CarUsedNewDetailFragment.EXTRA_VALUE, mContent);
        intent.putExtra(CarUsedNewDetailFragment.EXTRA_PARAMS, mParams);
        startActivityForResult(intent, REQUEST_DETAIL);
        return true;
      case R.id.menu_item_querys:
        //查询
        ExtraValueContent queryConten = new ExtraValueContent();
        queryConten.setExtName1(mBeginDate);
        queryConten.setExtName2(mEndDate);
        queryConten.setExtName3(mCarNumber);
        queryConten.setExtName4(mCarManName);
        Intent query = new Intent(getActivity(), CarUsedQueryActivity.class);
        query.putExtra(CarUsedQueryFragment.EXTRA_CONTENT, queryConten);
        startActivityForResult(query, REQUEST_QUERY);
        return true;
      default:
        return super.onOptionsItemSelected(item);
    }
  }

  @Override public String getUrl() {
    if (CarUsedParams.CAR_APPLY == mFrom) {
      //车辆申请下的登记列表
      return ApiConst.URL_CAR_LIST_APPLY;
    } else {
      return ApiConst.URL_CAR_USED_LIST;
    }
  }

  @Override public HashMap<String, String> getparameters() {
    ParamsNotEmpty params = new ParamsNotEmpty();
    params.put("memberCode", CurrentUser.newInstance(getActivity()).getMemberCode());
    if (CarUsedParams.CAR_APPLY == mFrom) {
      //车辆申请下的登记列表
      params.put("applyId", mMainId);
    } else {
      params.put("beginuseStartDate", mBeginDate);
      params.put("enduseStartDate", mEndDate);
      params.put("licensePlateNumber", mCarNumber);
      params.put("carManName", mCarManName);
      params.put("mainId", mMainId);
      params.put("mainName", mMainName);
    }

    return params.getParams();
  }

  @Override public void resolveResponse(String json) {
    CarUsedOriginal original = new Gson().fromJson(json, CarUsedOriginal.class);
    if (Base.SUCCESS == original.getResult() && original.getRows() != null) {
      if (getCurrentPage() == 1) {
        mlist.clear();
      }
      mlist.addAll(original.getRows());
      if (madapter == null) {
        madapter = new CarUsedListAdapter(getActivity(), mlist, CarUsedListAdapter.CAR_REGIST);
        setListAdapter(madapter);
      } else {
        madapter.notifyDataSetChanged();
      }
    } else {
      ToastUtil.show("获取列表失败");
    }
  }

  @Override public void onListItemClick(ListView l, View v, int position, long id) {
    //CarUsed content = mlist.get(position - 1);
    //Intent intent = new Intent(getActivity(), CarUsedDetailActivity.class);
    //intent.putExtra(CarUsedDetailFragment.EXTRA_VALUE, content);
    //startActivityForResult(intent, REQUEST_DETAIL);

    CarUsed content = mlist.get(position - 1);
    Intent intent = new Intent(getActivity(), CarUsedDetailActivity.class);
    intent.putExtra(CarUsedNewDetailFragment.EXTRA_VALUE, content);
    if (mParams != null) {
      mParams.setAdd(false);//详情
      intent.putExtra(CarUsedNewDetailFragment.EXTRA_PARAMS, mParams);
    }
    startActivityForResult(intent, REQUEST_DETAIL);
    super.onListItemClick(l, v, position, id);
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) {
      return;
    }
    if (requestCode == REQUEST_QUERY) {
      ExtraValueContent queryConten =
          (ExtraValueContent) data.getSerializableExtra(CarUsedQueryFragment.EXTRA_CONTENT);
      mBeginDate = queryConten.getExtName1();
      mEndDate = queryConten.getExtName2();
      mCarNumber = queryConten.getExtName3();
      mCarManName = queryConten.getExtName4();
    }
    refreshFromTop();//从顶部刷新
    volleyPost();
    super.onActivityResult(requestCode, resultCode, data);
  }
}
